import argparse

def read_text(file_path):
    """读取文本文件，返回每行非空的句子列表"""
    with open(file_path, 'r', encoding='utf-8') as f:
        lines = [line.strip() for line in f if line.strip()]
    return lines

def seconds_to_srt_time(seconds):
    """
    将浮点秒数转换为 SRT 时间格式: HH:MM:SS,mmm
    """
    total_ms = int(seconds * 1000)  # 总毫秒数
    hours = total_ms // 3600000
    remaining = total_ms % 3600000
    minutes = remaining // 60000
    remaining %= 60000
    seconds = remaining // 1000
    milliseconds = remaining % 1000
    return f"{hours:02d}:{minutes:02d}:{seconds:02d},{milliseconds:03d}"

def generate_subtitles(lines, speed, interval):
    """
    生成 SRT 字幕数据
    :param lines: 文本句子列表
    :param speed: 每分钟多少字
    :param interval: 句子之间的间隔时间（秒）
    """
    subtitles = []
    current_start = 0.0  # 当前句子的起始时间（秒）
    for i, line in enumerate(lines):
        word_count = len(line)
        duration = word_count * (60.0 / speed)  # 句子持续时间
        start_time = current_start
        end_time = start_time + duration
        subtitles.append((i + 1, start_time, end_time, line))
        current_start = end_time + interval  # 更新下一个句子的起始时间
    return subtitles

if __name__ == "__main__":
    # parser = argparse.ArgumentParser(description="将文本文件转换为 SRT 字幕")
    # parser.add_argument("input_file", help="输入文本文件路径（每行一个句子）")
    # parser.add_argument("speed", type=float, help="语速（每分钟多少字）")
    # parser.add_argument("interval", type=float, help="句子之间的间隔时间（秒）")
    # args = parser.parse_args()

    input_file_arg = r"C:\Users\HP\Videos\051901.txt"
    speed_arg = 260.5
    interval_arg = 0
    lines = read_text(input_file_arg)
    subtitles = generate_subtitles(lines, speed_arg, interval_arg )

    for index, start, end, text in subtitles:
        start_str = seconds_to_srt_time(start)
        end_str = seconds_to_srt_time(end)
        print(f"{index}")
        print(f"{start_str} --> {end_str}")
        print(text)
        print()  # 每个字幕条目之间空一行